package vip.xiaonuo.app.modular.appuser.controller.front;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.app.modular.appuser.entity.AppUser;
import vip.xiaonuo.app.modular.appuser.param.front.ApiAppUserParam;
import vip.xiaonuo.app.modular.appuser.param.front.ApiGetUserPageParam;
import vip.xiaonuo.app.modular.appuser.result.ApiUserPageResult;
import vip.xiaonuo.app.modular.appuser.service.AppUserService;
import vip.xiaonuo.app.modular.auth.service.AppAuthService;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.pojo.login.AppLoginUser;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title : 用户信息表</p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/10/7 11:12
 */
@RestController
@RequestMapping("/api/user")
public class ApiAppUserController {

    @Resource
    private AppUserService appUserService;
    @Resource
    private SysFileInfoService sysFileInfoService;
    @Resource
    private AppAuthService appAuthService;

    /**
     * 修改用户信息
     *
     * @param param
     * @return
     */
    @PostMapping("/updateUser")
    public ResponseData updateUser(@RequestBody ApiAppUserParam param) {

        appUserService.updateUser(param);
        return ResponseData.success();
    }


    /**
     * 首页列表
     */
    @GetMapping("/getUserPage")
    public ResponseData getUserPage(ApiGetUserPageParam param) {

        //逻辑说明：男用户返回女的（数据号），女的返回男的
        Integer sex = LoginContextHolder.me().getSysLoginUser().getSex();
        if (ObjectUtil.isNotEmpty(sex)) {

            if (sex.equals(1)) {
                param.setIsReal(0);
            }
            if (sex.equals(2)) {
                param.setIsReal(1);
            }
        }
        //查询数据
        PageResult<AppUser> userPage = appUserService.getUserPage(param);

        //前端返回数据
        List<ApiUserPageResult> results = new ArrayList<>();

        //循环数据
        userPage.getRows().forEach(e -> {
            ApiUserPageResult result = new ApiUserPageResult();
            result.setId(e.getId());//id
            result.setNick(e.getNick());//昵称
            result.setSex(e.getSex());//性别
            result.setTxCode(e.getTxCode());//腾讯code
            result.setOnlineStatus(e.getOnlineStatus());//在线状态
            String headPicUrl = sysFileInfoService.getSysFileInfo(e.getHeadPicId());
            result.setHeadPicUrl(headPicUrl);//头像
            results.add(result);
        });
        return ResponseData.success(results);
    }

    /**
     * 根据腾讯Code获取用户信息详情
     */
    @GetMapping("/getUserInfoByTxCode")
    public ResponseData getUserInfoByTxCode(String txCode) {
        AppUser appUser = appUserService.getUserInfoTxCode(txCode);
        AppLoginUser appLoginUser = appAuthService.genSysLoginCustomer(appUser);
        return ResponseData.success(appLoginUser);
    }


//    /**
//     * 根据腾讯Code获取用户信息详情
//     */
//    @GetMapping("/getUserInfoByTxCode")
//    public ResponseData getUserInfoByTxCode(String txCode) {
//
//        AppUser appUser = appUserService.getUserInfoTxCode(txCode);
//
//        UserInfoByIdResult result = new UserInfoByIdResult();
//        BeanUtil.copyProperties(appUser, result);
//        //头像
//        String headPicUrl = sysFileInfoService.getSysFileInfo(appUser.getHeadPicId());
//        result.setHeadPicUrl(headPicUrl);
//        //形象url
//        if (ObjectUtil.isNotEmpty(appUser.getIntroPicIds())) {
//            List<String> introUls = sysFileInfoService.getListSysFileInfo(Arrays.asList(appUser.getIntroPicIds().split(",")));
//            result.setIntroPicIdUrl(introUls);
//        }
//        //年龄 星座
//        if (ObjectUtil.isNotEmpty(appUser.getBirthday())) {
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            String format = simpleDateFormat.format(appUser.getBirthday());
//            DateTime dateTime = DateUtil.parseDate(format);
//            result.setAge(DateUtil.ageOfNow(dateTime));
//            result.setConstellation(ConstellationUtil.getConstellation(format));
//
//        }
//        return ResponseData.success(result);
//    }


}
